package router

import (
	"example.com/m/v2/controllers"
	"example.com/m/v2/docs"
	"example.com/m/v2/middleware"
	"github.com/gin-gonic/gin"
	swaggerFiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
	"net/http"
)

func APIRouter(r gin.IRouter) {
	//r := gin.Default()
	registerSwagger(r)
	//v1 := r.Group("//v1")
	//api := v1.Group("/api")
	r.Use(
		middleware.NewLoginJWTMiddlewareBuilder().
			IgnorePaths("/api/v1/users/login").
			IgnorePaths("/api/v1/users/GetCalendarInfo").
			Build())
	{
		v1 := r.Group("/api/v1")
		userController := &controllers.UserController{}
		equipmentController := &controllers.EquipmentController{}
		SetupUserRoutes(v1, userController)
		SetupEquipmentRoutes(v1, equipmentController)
	}
	//return r
}

func registerSwagger(r gin.IRouter) {
	// API文档访问地址: http://host/swagger/index.html
	// 注解定义可参考 https://github.com/swaggo/swag#declarative-comments-format
	// 样例 https://github.com/swaggo/swag/blob/master/example/basic/api/api.
	docs.SwaggerInfo.BasePath = "/api/v1"
	docs.SwaggerInfo.Title = "Go-Gin-Api"
	docs.SwaggerInfo.Description = "使用Go语言实现一个API"
	docs.SwaggerInfo.Version = "1.0"
	//docs.SwaggerInfo.Host = "*:8080"
	docs.SwaggerInfo.Schemes = []string{"http", "https"}

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	r.GET("/", func(context *gin.Context) {
		context.Redirect(http.StatusMovedPermanently, "/swagger/index.html")
	})
}
